package com.agregatesdk.core;

import net.miidi.wall.AdWall;
import net.miidi.wall.IAdWallGetPointsNotifier;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import cn.waps.AppConnect;
import cn.waps.UpdatePointsNotifier;

import com.agregatesdk.point.PointManager;
import com.newqm.sdkoffer.QuMiNotifier;
import com.newqm.sdkoffer.QuMiOfConnect;

public class ActiveListener extends BroadcastReceiver {

	@Override
	public void onReceive(Context arg0, final Intent intent) {
		// TODO Auto-generated method stub
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				final Activity context = SDKListView.context;
				if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
					
					// 设置激活监听器
					AppConnect.getInstance(context).getPoints(new UpdatePointsNotifier() {

						@Override
						public void getUpdatePointsFailed(String arg0) {

							context.runOnUiThread(new Runnable() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									Toast.makeText(context, "服务器异常", Toast.LENGTH_LONG).show();
								}
							});

						}

						@Override
						public void getUpdatePoints(String arg0, int total) {
							
							Log.d("fcz:", "waps total: " + total);
							
							if (total == 0) {
								return;
							}
							
							AppConnect.getInstance(context).spendPoints(total, this);
							
							AppConnect.getInstance("e52aac974bdb41a46df8a0f69cf6f4ee", "WAPS", context);
							AppConnect.getInstance(context).setAdViewClassName("com.adview.adwall.adapters.WapsAdView");
							
							
							final float credit = PointManager.dealWithCredit(context, total, Constant.SDK_TYPE_WAPS);
							if (credit > 0 && credit < 40) {
								PointManager.sendCredit2Server(context, credit, Constant.SDK_TYPE_WAPS);

							}

						}
					});
					
					
					// 迷迪
					AdWall.getPoints(new IAdWallGetPointsNotifier() {

						@Override
						public void onReceivePoints(String arg0, int total) {
							
							Log.d("fcz:", "midii total: " + total);
							
							if (total == 0) {
								return;
							}
							
							AdWall.spendPoints(total, null);
							final float credit = PointManager.dealWithCredit(context, total, Constant.SDK_TYPE_MIDII);
							if (credit > 0 && credit < 40) {
								PointManager.sendCredit2Server(context, credit, Constant.SDK_TYPE_MIDII);

							}
						}

						@Override
						public void onFailReceivePoints() {

							context.runOnUiThread(new Runnable() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									Toast.makeText(context, "服务器异常", Toast.LENGTH_LONG).show();
								}
							});

						}
					});
					
					
					QuMiOfConnect.getQumiConnectInstance().showpoints(new QuMiNotifier() {
						
						@Override
						public void getUpdatePointsFailed(String arg0) {
							context.runOnUiThread(new Runnable() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									Toast.makeText(context, "服务器异常", Toast.LENGTH_LONG).show();
								}
							});
						}
						
						@Override
						public void getUpdatePoints(int total) {
							
							Log.d("fcz:", "qumi total: " + total);
							
							if (total == 0) {
								return;
							}
							
							QuMiOfConnect.getQumiConnectInstance().spendPoints(new QuMiNotifier() {
                                
                                @Override
                                public void getUpdatePointsFailed(String arg0) {
                                    // TODO Auto-generated method stub
                                    
                                }
                                
                                @Override
                                public void getUpdatePoints(int arg0) {
                                    // TODO Auto-generated method stub
                                    
                                }
                            }, total);
							final float credit = PointManager.dealWithCredit(context, total, Constant.SDK_TYPE_QUMI);
							if (credit > 0 && credit < 40) {
								PointManager.sendCredit2Server(context, credit, Constant.SDK_TYPE_QUMI);

							}
						}
						
					});
				}
			}
		}).start();
	}

}
